package com.linchong.linkedlist.easy;

import com.linchong.linkedlist.basic.BasicLinkList;

/**
 * @author linchong
 * @version 1.0
 * @Date: 2020-12-10 0:29
 * @Description: RemoveDuplicatesFromSortedList$
 * 给定一个排序链表，删除所有重复的元素，使得每个元素只出现一次。
 *
 * 示例 1:
 *
 * 输入: 1->1->2
 * 输出: 1->2
 * 示例 2:
 *
 * 输入: 1->1->2->3->3
 * 输出: 1->2->3
 * 通过次数164,032提交次数317,080
 *
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list
 *
 */
@SuppressWarnings("unused")
public class RemoveDuplicatesFromSortedList extends BasicLinkList {
	public ListNode deleteDuplicates(ListNode head) {
		if(head==null||head.next==null){
			return head;
		}
		ListNode cur = head;
		while(cur.next!=null){
			ListNode pNext = cur.next;
			if(cur.val==pNext.val){
				while(pNext!=null && pNext.val==cur.val){
					pNext=pNext.next;
				}
				cur.next = pNext;
			}else{
				cur = pNext;
			}
		}
		return head;
	}
}
